<template>
	<view class="chongzhi">
		<headertop title="充值" str1="oridei" :back1="back1"></headertop>
		<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11448@2x.png" class="chongzhiback"
			mode=""></image>
		<view class="chongzhiright" @click="gopage('充值记录')">
			<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x(48).png" mode=""></image>
			<span>充值记录</span>
		</view>
		<image src="/static/wxdownload 88@2x.png" class="img1" mode="">
		</image>
		<view class="yuexianshi">
			<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/微信图片_20250429092224.png" class="img2"
				mode=""></image>
			<view class="num">
				{{moneyobj.money}}
			</view>
		</view>
		<view class="bottombox">
			<view class="bottombox_center">
				<view class="inputbox">
					<image src="/static/wwxximage@2x (3).png" mode="aspectFill"></image>
					<input type="digit" v-model="money" placeholder="请输入自定义充值金额" />
				</view>
				<view class="itemboxss">
					<view class="itembox" :style="{'border':active==index?'':'none'}"
						v-for="(item,index) in moneyobj.order" :key="index" @click="qiehuan(index)">
						<view class="lefttopbox">
							<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/微信图片_20250429094740.png"
								class="lefttopboximg" mode=""></image>
							<view class="text">
								赠送{{item.giftnum}}
							</view>
						</view>
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/微信图片_20250429095919.png"
							v-if="active==index" class="xzbox" mode=""></image>
						<image src="/static/jbimage 86@2x.png" class="imgh" mode=""></image>
						<view class="btn">
							￥{{item.money}}
						</view>
					</view>
					<!-- 		<view class="itembox" style="border: none;" v-for="item in 5">
						<view class="lefttopbox">
							<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/微信图片_20250429094740.png" class="lefttopboximg" mode=""></image>
							<view class="text">
								赠送10个
							</view>
						</view>
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11315@2x.png" class="imgh" mode=""></image>
						<view class="btn">
							￥200.00
						</view>
					</view> -->
				</view>
			</view>
			<view class="" style="height: 184rpx;">

			</view>
		</view>
		<view class="fixedbox">
			<view class="centerbox" @click="lijikaitong">
				立即充值
			</view>
		</view>
	</view>
</template>

<script setup>
	import headertop from '@/components/header.vue'
	import {
		onPageScroll,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		moneylist,
		rechargeorder
	} from '@/api/my.js'
	import {
		ref,
		watch
	} from 'vue'
	const back1 = ref('')
	const active = ref(-1)
	const moneyobj = ref({})
	const money = ref('')
	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#84BD78'
		} else {
			back1.value = ''
		}
	})
	onLoad(() => {
		init()
	})
	watch(money, (newval, oldval) => {
		if (newval) {
			var ind = moneyobj.value.order.findIndex(item => item.money * 1 == newval * 1)
			active.value = ind
		}
	})
	async function init() {
		const res = await moneylist()
		if (res.data.code == 1) {

			moneyobj.value = res.data.data

		}
	}
	async function lijikaitong() {
		if (money.value < 0 || !money.value) {
			uni.showToast({
				title: '充值金额必须大于0且不能为空',
				icon: 'none'
			})
			return
		}
		const res = await rechargeorder({
			money: money.value
		})
		if (res.data.code == 1) {

			var payobj = res.data.data.pay
			console.log(payobj, 'res。data---------');
			uni.requestPayment({
				timeStamp: String(payobj.timestamp),
				nonceStr: payobj.noncestr,
				package: payobj.package,
				signType: 'MD5',
				paySign: payobj.sign,
				success(res) {
					setTimeout(() => {
						uni.showToast({
							title: '充值成功',
							icon: 'none'
						})
					}, 500)
					money.value = ''
					uni.navigateBack()
				},
				fail(e) {
					uni.showToast({
						title: '支付失败',
						icon: 'none'
					})
					console.log('支付失败--------------', e);
				}
			})

		} else {
			uni.showToast({
				title: res.data.msg,
				icon: 'none'
			})
		}
	}

	function qiehuan(ind) {
		active.value = ind
		money.value = moneyobj.value.order[ind]['money']
	}

	function gopage(str) {
		if (str == '充值记录') {
			uni.navigateTo({
				url: '/sub_my/my/chongzhirecord'
			})
		}
	}
</script>

<style lang="scss">
	.chongzhi {
		width: 100%;
		overflow: hidden;
		position: relative;

		.fixedbox {
			width: 100%;
			height: 184rpx;
			background: #FFFFFF;
			box-shadow: 0rpx -4rpx 4rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			overflow: hidden;
			position: fixed;
			bottom: 0%;

			.centerbox {
				width: 686rpx;
				height: 88rpx;
				background: #7EBC79;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				text-align: center;
				line-height: 88rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #FFFFFF;
				margin: 22rpx auto;
			}
		}

		.chongzhiright {
			width: 178rpx;
			height: 54rpx;
			background: #7EBC79;
			border-radius: 27rpx 0rpx 0rpx 27rpx;
			position: absolute;
			right: 0%;
			top: 0%;
			display: flex;
			align-items: center;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #FFFFFF;
			margin-top: 180rpx;

			image {
				width: 32rpx;
				height: 32rpx;
				margin-left: 16rpx;
				margin-right: 4rpx;
			}
		}

		.chongzhiback {
			position: absolute;
			width: 100%;
			height: 816rpx;
			z-index: -1;
		}

		.img1 {
			width: 328rpx;
			height: 328rpx;
			margin: auto;
			margin-top: 202rpx;
			display: block;
		}

		.bottombox {
			width: 100%;
			overflow: hidden;
			background: linear-gradient(180deg, #A5D39C 0%, #FFFFFF 100%);
			border-radius: 48rpx 48rpx 0rpx 0rpx;
			margin-top: 26rpx;

			&_center {
				width: 686rpx;
				margin: auto;
				margin-top: 32rpx;

				.itemboxss .itembox:nth-child(3n) {
					margin-right: 0 !important;
				}

				.itemboxss {
					width: 100%;
					// overflow: hidden;
					margin-top: 50rpx;
					display: flex;
					flex-wrap: wrap;

					.itembox {
						width: 210rpx;
						height: 230rpx;
						background: #FFFFFF;
						box-shadow: 0rpx 10rpx 15rpx 0rpx rgba(92, 165, 73, 0.58);
						border-radius: 24rpx 24rpx 24rpx 24rpx;
						border: 2rpx solid #499043;
						display: flex;
						flex-direction: column;
						align-items: center;
						position: relative;
						margin-right: 24rpx;
						margin-bottom: 24rpx;
						box-sizing: border-box;

						.lefttopbox {
							position: absolute;
							left: -8%;
							top: -8%;
							width: 126rpx;
							height: 38rpx;
							text-align: center;
							line-height: 38rpx;
							z-index: 9999;

							.lefttopboximg {
								width: 126rpx;
								height: 38rpx;
								position: absolute;
								left: 0%;
								top: 0%;
								z-index: -1;
							}

							.text {
								z-index: 999;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #1D2129;
							}
						}

						.xzbox {
							width: 32rpx;
							height: 32rpx;
							position: absolute;
							right: 8%;
							top: 8%;
						}

						.imgh {
							width: 170rpx;
							height: 72rpx;
							margin-top: 46rpx;
						}

						.btn {
							width: 174rpx;
							height: 48rpx;
							background: linear-gradient(360deg, #68C155 0%, #ACF09E 100%);
							border-radius: 12rpx 12rpx 12rpx 12rpx;
							text-align: center;
							line-height: 48rpx;
							font-weight: bold;
							font-size: 28rpx;
							color: #1D2129;
							margin-top: 24rpx;
						}
					}
				}

				.inputbox {
					width: 100%;
					height: 152rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 4rpx 7rpx 0rpx rgba(134, 202, 115, 0.43);
					border-radius: 24rpx 24rpx 24rpx 24rpx;
					display: flex;
					align-items: center;

					image {
						width: 108rpx;
						height: 108rpx;
						margin-left: 24rpx;
					}

					input {
						font-size: 32rpx;
						margin-left: 22rpx;
					}
				}
			}
		}

		.yuexianshi {
			width: 312rpx;
			margin: auto;
			margin-top: 16rpx;
			overflow: hidden;
			position: relative;

			.img2 {
				display: block;
				width: 312rpx;
				height: 142rpx;
			}

			.num {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #FFFFFF;
				position: absolute;
				z-index: 999;
				bottom: 2%;
				margin-left: 86rpx;
			}
		}

	}
</style>